package com.jawmask.android.discover.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.location.Address;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jawmask.android.discover.R;
import com.jawmask.android.discover.bean.GPSLocation;
import com.jawmask.android.discover.bean.SearchRequest;
import com.jawmask.android.discover.bean.SearchResult;
import com.jawmask.android.discover.bean.task.FetchImageTask;
import com.jawmask.android.discover.bean.task.PlaceDetailTask;
import com.jawmask.android.discover.utils.UserSession;
import com.jawmask.android.discover.utils.ValidationUtils;

public class SearchResultDetails extends BaseActivity implements OnClickListener {

	private TextView searchPlaceAddress, searchPlaceWebUrl, searchPlaceMapUrl, searchPlaceContactNumber;
	private ImageView searchPlaceLogoImage;
	private Button searchPlaceGetDirectionButton;
	private SearchResult searchResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_result_detail);

		searchPlaceAddress = (TextView) findViewById(R.id.searchPlaceAddress);
		searchPlaceWebUrl = (TextView) findViewById(R.id.searchPlaceWebUrl);
		searchPlaceMapUrl = (TextView) findViewById(R.id.searchPlaceMapUrl);
		searchPlaceContactNumber = (TextView) findViewById(R.id.searchPlaceContactNumner);
		searchPlaceLogoImage = (ImageView) findViewById(R.id.searchPlaceItemLogo);
		searchPlaceGetDirectionButton = (Button) findViewById(R.id.searchPlaceGetMapDirection);
		searchPlaceGetDirectionButton.setOnClickListener(this);

		searchResult = (SearchResult) getIntent().getSerializableExtra(getString(R.string.extra_key_search_plcae_item));

		waitForData(false);

		placeSearchDetailTask.execute(getSearchRequest());
	}

	private SearchRequest getSearchRequest() {
		SearchRequest searchRequest = new SearchRequest();

		searchRequest.setLocationFromDevice(true);
		searchRequest.setReferenceNumber(searchResult.getReference());

		return searchRequest;
	}

	private void populateDetails(final SearchResult searchResult) {
		String noDetailAvailableMsg = getString(R.string.no_details_are_available);

		if (!ValidationUtils.isNullOrEmpty(searchResult.getFullAddress()))
			searchPlaceAddress.setText(searchResult.getFullAddress());
		else
			searchPlaceAddress.setText(noDetailAvailableMsg);

		if (!ValidationUtils.isNullOrEmpty(searchResult.getWebsite()))
			searchPlaceWebUrl.setText(searchResult.getWebsite());
		else
			searchPlaceWebUrl.setText(noDetailAvailableMsg);

		if (!ValidationUtils.isNullOrEmpty(searchResult.getMapUrl()))
			searchPlaceMapUrl.setText(searchResult.getMapUrl());
		else
			searchPlaceMapUrl.setText(noDetailAvailableMsg);

		if (!ValidationUtils.isNullOrEmpty(searchResult.getInternationalPhoneNumber()))
			searchPlaceContactNumber.setText(searchResult.getInternationalPhoneNumber());
		else
			searchPlaceContactNumber.setText(noDetailAvailableMsg);

		new FetchImageTask(SearchResultDetails.this, searchPlaceLogoImage).execute(searchResult.getIconPath());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.searchPlaceGetMapDirection:

			GPSLocation searchPlaceItemGPSLocation = searchResult.getLocation();
			Address userAddress = UserSession.getInstance().getUserAddress();

			StringBuffer urlString = new StringBuffer("http://maps.google.com/maps?");
			urlString.append("f=d&saddr=");
			urlString.append(userAddress.getLatitude());
			urlString.append(",");
			urlString.append(userAddress.getLongitude());
			urlString.append("&daddr=");
			urlString.append(searchPlaceItemGPSLocation.getLatitude());
			urlString.append(",");
			urlString.append(searchPlaceItemGPSLocation.getLongitude());

			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString.toString()));
			intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
			startActivity(intent);
			break;
		}
	}

	private final PlaceDetailTask placeSearchDetailTask = new PlaceDetailTask(SearchResultDetails.this) {

		protected void onPostExecute(SearchResult searchResult) {
			receivedData();

			if (searchResult != null)
				populateDetails(searchResult);
		};
	};
}
